# -*- coding: utf-8 -*-
# @Time : 2020/8/15 15:17
# @Author : yongf
# @File : ProxyCheck.py
# @Software: PyCharm

from model.ips import *
from threading import Thread


def check():
    # 当前有多少条代理
    count = ips.count(ips)
    # 每次获取20条代理检查
    page = int(count / 20)  # 总页数
    for i in range(0, page):
        limit = i * 20
        data = ips.selectAll(ips, 'id asc', limit, 20)
        for item in data:
            if len(item) > 1:
                ip = item[1]
                port = int(item[2])
                if not checkProxyValid(ip, port):
                    logging.info('代理%s:%d已失效，从数据库中删除。。。' % (ip, port))
                    ips.delete(ips, ip, port)
        # 每5秒拉取一次数据库
        time.sleep(5)


if __name__ == '__main__':
    while True:
        Thread(target=check).start()
        # 每刷新一次，1小时后在次检查
        time.sleep(3600)
